<?php

namespace App\Jobs;

use App\Notifications\Wechat\LevelEvaluate;
use App\Internals\UserInternal;
use App\Internals\StudyInternal;
use Illuminate\Support\Facades\Log;

class LevelEvaluateJob extends Job
{
    protected $openId;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($openId)
    {
        $this->openId = $openId;
    }

    public function handle() {
        Log::info('LevelEvaluateJob handle start');
        // 用openid获取用户信息
        $userInfo = UserInternal::getByOpenId($this->openId);
        if (!$userInfo) {
            Log::info('LevelEvaluateJob getByOpenId: no user');
            return;
        }

        // 判断是否推送评测消息，如果是，则返回评测相关信息
        $res = StudyInternal::ifPushLevelEvaluate($userInfo['userId']);
        if (!$res) {
            Log::info('LevelEvaluateJob ifPushLevelEvaluate: false');
            return;
        }

        Log::info('LevelEvaluateJob notify start');
        $courseEnd = new LevelEvaluate();
        $courseEnd->notify(
            $this->openId,
            $userInfo['nickname'],
            $res['wordCount']
        );
        Log::info('LevelEvaluateJob notify end');
        Log::info('LevelEvaluateJob handle end');
    }
}
